/**
  ******************************************************************************
  * @file USART/HyperTerminal_HwFlowControl/stm32f10x_it.c 
  * @author  MCD Application Team
  * @version  V3.0.0
  * @date  04/06/2009
  * @brief  Main Interrupt Service Routines.
  *         This file provides template for all exceptions handler and 
   *        peripherals interrupt service routine.
  ******************************************************************************
  * @copy
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT 2009 STMicroelectronics</center></h2>
  */ 

/* Includes ------------------------------------------------------------------*/
#include "stm32f10x_it.h"
#include "stm32f10x_tim.h"
#include "stm32f10x_exti.h"
#include "appli.h"

/** @addtogroup StdPeriph_Examples
  * @{
  */

/** @addtogroup USART_HyperTerminal_HwFlowControl
  * @{
  */ 

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/******************************************************************************/
/*            Cortex-M3 Processor Exceptions Handlers                         */
/******************************************************************************/

/**
  * @brief  This function handles NMI exception.
  * @param  None
  * @retval : None
  */
void NMI_Handler(void)
{
}

/**
  * @brief  This function handles Hard Fault exception.
  * @param  None
  * @retval : None
  */
void HardFault_Handler(void)
{
  /* Go to infinite loop when Hard Fault exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles Memory Manage exception.
  * @param  None
  * @retval : None
  */
void MemManage_Handler(void)
{
  /* Go to infinite loop when Memory Manage exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles Bus Fault exception.
  * @param  None
  * @retval : None
  */
void BusFault_Handler(void)
{
  /* Go to infinite loop when Bus Fault exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles Usage Fault exception.
  * @param  None
  * @retval : None
  */
void UsageFault_Handler(void)
{
  /* Go to infinite loop when Usage Fault exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles SVCall exception.
  * @param  None
  * @retval : None
  */
void SVC_Handler(void)
{
}

/**
  * @brief  This function handles Debug Monitor exception.
  * @param  None
  * @retval : None
  */
void DebugMon_Handler(void)
{
}

/**
  * @brief  This function handles PendSV_Handler exception.
  * @param  None
  * @retval : None
  */
void PendSV_Handler(void)
{
}

/**
  * @brief  This function handles SysTick Handler.
  * @param  None
  * @retval : None
  */
void SysTick_Handler(void)
{
}



/******************************************************************************/
/*                 STM32F10x Peripherals Interrupt Handlers                   */
/*  Add here the Interrupt Handler for the used peripheral(s) (PPP), for the  */
/*  available peripheral interrupt handler's name please refer to the startup */
/*  file (startup_stm32f10x_xx.s).                                            */
/******************************************************************************/

/**
  * @brief  This function handles PPP interrupt request.
  * @param  None
  * @retval : None
  */
/*void PPP_IRQHandler(void)
{
}*/

extern void power_delay();
#define CR1_CEN_Set                 ((uint16_t)0x0001)
void TIM2_IRQHandler(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;  
  
    if (TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET){
      TIM_ClearITPendingBit(TIM2, TIM_IT_CC1);
      switch(output_state){
      case OUTPUT_CARRIER:
        if (--num_capture_codes == 0)
          output_state = OUTPUT_END;
        else
          output_state = OUTPUT_LOW;            /* prepare the next wave */

        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
        GPIO_Init(GPIOB, &GPIO_InitStructure); 
        GPIO_ResetBits(GPIOB, GPIO_Pin_1);       /* stop to output the 38K carrier wave */

        TIM2->CCR1 = *pRmtCtrlCodeBuf++;         /* set the value to compare */
        TIM2->EGR = 0x01;                        /* initiate the count */           
        break;
      case OUTPUT_LOW:
        if (--num_capture_codes == 0)
          output_state = OUTPUT_END;
        else
          output_state = OUTPUT_CARRIER;

        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_Init(GPIOB, &GPIO_InitStructure);    /* start to output the 38K carrier wave */     

        TIM2->CCR1 = *pRmtCtrlCodeBuf++;          /* set the value to compare */        
        TIM2->EGR = 0x01;                         /* initiate the count */           
        break;
      case OUTPUT_END:
        TIM3->CR1 &= ~CR1_CEN_Set;            /* Disable the TIM3 Counter */
        TIM2->CR1 &= ~CR1_CEN_Set;            /* Disable the TIM2 Counter */
        global_state = GLOBAL_IDLE;  
        break;
      }
    }
}



/**
  * @brief  This function handles TIM3 global interrupt request.
  * @param  None
  * @retval : None
  */
void TIM3_IRQHandler(void)
{
  if (TIM_GetITStatus(TIM3, TIM_IT_CC3) != RESET){
    TIM_ClearITPendingBit(TIM3, TIM_IT_CC3);
    switch (capture_state){
    case CAPTURE_START:
      capture_state = CAPTURE_RISING;
      TIM3->CCER = 0x0100; /* channel3 capture enable, rising edge */
      TIM3->EGR = 0x01; /* initiate the count */    
      TIM4->EGR = 0x01; /* initiate the count */          
      TIM4->CR1 |= CR1_CEN_Set;  /* enable tim4 counter */
      break;
    case CAPTURE_RISING:
      *pRmtCtrlCodeBuf++ = TIM_GetCapture3(TIM3);
      num_capture_codes++;
      capture_state = CAPTURE_FALLING;      
      TIM3->CCER = 0x0300; /* channel3 capture enable, falling edge */
      TIM3->EGR = 0x01; /* initiate the count */          
      TIM4->EGR = 0x01; /* initiate the count */    
      break;
    case CAPTURE_FALLING:
      *pRmtCtrlCodeBuf++ = TIM_GetCapture3(TIM3);
      num_capture_codes++;
      capture_state = CAPTURE_RISING;            
      TIM3->CCER = 0x0100; /* channel3 capture enable, rising edge */
      TIM3->EGR = 0x01; /* initiate the count */    
      TIM4->EGR = 0x01; /* initiate the count */          
      break;
    }
  }
}


/**
  * @brief  This function handles TIM4 global interrupt request.
  * @param  None
  * @retval : None
  */
void TIM4_IRQHandler(void)
{
  if (TIM_GetITStatus(TIM4, TIM_IT_CC1) != RESET){
    TIM_ClearITPendingBit(TIM4, TIM_IT_CC1);      /* the capture of the Irda signals is end */
    TIM4->CR1 &= ~CR1_CEN_Set;                    /* disable tim4 counter */
    global_state = GLOBAL_CAPTURE_END;
  }
}

/**
  * @brief  This function handles EXTI9-5 global interrupt request.
  * @param  None
  * @retval : None
  */
void EXTI9_5_IRQHandler(void)
{
 
  if(EXTI_GetITStatus(EXTI_Line7) != RESET){  /* PB7 used to handle the user interface  */ 
    EXTI_ClearITPendingBit(EXTI_Line7);   
    switch(global_state){
    case GLOBAL_IDLE:
      global_state = GLOBAL_CAPTURE;          /* waiting for receiving the ifrd code */
      Timer_Configuration();                  /* initiate the timer to calculate the interval between two ifrd signals */
      Irda_In_Capture_Configuration();        /* initiate the timer to capture the ifrd signals */
      break;
    case GLOBAL_CAPTURE:
      break;                                        /* no action */
    case GLOBAL_CAPTURE_END:
      global_state = GLOBAL_REPEAT;                       /* start to output the ifrd signals */
      carrier_38K_configuration();            /* initiate the timer to generate the 38K carrier wave */
      Irda_Out_Compare_Configuration();       /* output the ifrd signals captured by the previous course */
      break;
    case GLOBAL_REPEAT:
      break;                                  /* no action */
    }
  }
}


/**
  * @}
  */ 

/******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/
